<template>
  <div class="config-container">

    <div class="config-content">
      <!-- 左侧导航 -->
      <div class="sidebar">
        <el-menu
          :default-active="activeSection.toString()"
          class="config-menu"
          @select="handleMenuSelect"
        >
          <el-menu-item
            v-for="(section, index) in sections"
            :key="index"
            :index="index.toString()"
          >
            <el-icon>
              <component :is="section.icon" />
            </el-icon>
            <span>{{ section.title }}</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 右侧配置区域 -->
      <div class="main-content">
        <!-- 基本设置 -->
        <div v-show="activeSection === 0" class="config-section">
          <h2 class="section-title">基本设置</h2>

          <el-form :model="config" label-width="120px" label-position="top">
            <el-form-item label="网站名称">
              <el-input v-model="config.siteName" placeholder="输入网站名称" />
            </el-form-item>

            <el-form-item label="网站Logo">
              <div class="logo-upload">
                <div
                  class="logo-preview"
                  :style="{ backgroundImage: `url(${config.siteLogo})` }"
                >
                  <div v-if="!config.siteLogo" class="logo-placeholder">
                    <el-icon :size="40"><Picture /></el-icon>
                    <span>上传Logo</span>
                  </div>
                </div>
                <el-button
                  type="primary"
                  @click="triggerLogoUpload"
                  class="upload-btn"
                >
                  <el-icon><Upload /></el-icon>
                  <span>选择图片</span>
                </el-button>
                <input
                  type="file"
                  ref="logoInput"
                  accept="image/*"
                  @change="handleLogoUpload"
                  class="file-input"
                />
              </div>
            </el-form-item>

            <el-form-item label="网站描述">
              <el-input
                v-model="config.siteDescription"
                type="textarea"
                placeholder="输入网站描述"
                :rows="3"
              />
            </el-form-item>

            <el-form-item label="网站关键词">
              <el-input
                v-model="config.siteKeywords"
                placeholder="输入关键词，用逗号分隔"
              />
            </el-form-item>
          </el-form>
        </div>

        <!-- 主题设置 -->
        <div v-show="activeSection === 1" class="config-section">
          <h2 class="section-title">主题设置</h2>

          <el-form :model="config" label-width="120px" label-position="top">
            <el-form-item label="主色调">
              <div class="color-picker">
                <el-color-picker v-model="config.primaryColor" />
                <div class="color-value">{{ config.primaryColor }}</div>
              </div>
            </el-form-item>

            <el-form-item label="辅色调">
              <div class="color-picker">
                <el-color-picker v-model="config.secondaryColor" />
                <div class="color-value">{{ config.secondaryColor }}</div>
              </div>
            </el-form-item>

            <el-form-item label="主题模式">
              <div class="theme-toggle">
                <el-radio-group v-model="config.themeMode">
                  <el-radio-button label="light">
                    <el-icon><Sunny /></el-icon>
                    <span>浅色模式</span>
                  </el-radio-button>
                  <el-radio-button label="dark">
                    <el-icon><Moon /></el-icon>
                    <span>深色模式</span>
                  </el-radio-button>
                </el-radio-group>
              </div>
            </el-form-item>

            <el-form-item label="字体设置">
              <el-select v-model="config.fontFamily">
                <el-option value="'Segoe UI', sans-serif" label="Segoe UI" />
                <el-option value="'Roboto', sans-serif" label="Roboto" />
                <el-option value="'Open Sans', sans-serif" label="Open Sans" />
                <el-option value="'Poppins', sans-serif" label="Poppins" />
                <el-option
                  value="'Montserrat', sans-serif"
                  label="Montserrat"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>

        <!-- SEO设置 -->
        <div v-show="activeSection === 2" class="config-section">
          <h2 class="section-title">SEO设置</h2>

          <el-form :model="config" label-width="120px" label-position="top">
            <el-form-item label="网站标题">
              <el-input v-model="config.seoTitle" placeholder="输入网站标题" />
            </el-form-item>

            <el-form-item label="Meta描述">
              <el-input
                v-model="config.metaDescription"
                type="textarea"
                placeholder="输入Meta描述..."
                :rows="3"
              />
            </el-form-item>

            <el-form-item label="Meta关键词">
              <el-input
                v-model="config.metaKeywords"
                placeholder="输入Meta关键词"
              />
            </el-form-item>

            <el-form-item label="Google验证码">
              <el-input
                v-model="config.googleVerification"
                placeholder="输入Google验证码"
              />
            </el-form-item>
          </el-form>
        </div>

        <!-- 社交链接 -->
        <div v-show="activeSection === 3" class="config-section">
          <h2 class="section-title">社交链接</h2>

          <div class="social-links">
            <div
              v-for="(social, index) in config.socialLinks"
              :key="index"
              class="social-item"
            >
              <div class="social-icon">
                <el-icon :size="24">
                  <component :is="getSocialIcon(social.platform)" />
                </el-icon>
              </div>
              <el-input
                v-model="social.url"
                :placeholder="`输入${social.platform}链接`"
                class="social-input"
              />
              <el-button
                class="remove-btn"
                type="danger"
                circle
                @click="removeSocialLink(index)"
              >
                <el-icon><Close /></el-icon>
              </el-button>
            </div>

            <el-button
              class="add-social-btn"
              type="primary"
              @click="addSocialLink"
            >
              <el-icon><Plus /></el-icon>
              <span>添加社交链接</span>
            </el-button>
          </div>
        </div>

        <!-- 高级设置 -->
        <div v-show="activeSection === 4" class="config-section">
          <h2 class="section-title">高级设置</h2>

          <el-form :model="config" label-width="120px" label-position="top">
            <el-form-item label="Google Analytics">
              <el-input
                v-model="config.googleAnalytics"
                placeholder="输入Google Analytics ID"
              />
            </el-form-item>

            <el-form-item label="自定义头部代码">
              <el-input
                v-model="config.customHead"
                type="textarea"
                placeholder="输入自定义头部代码..."
                :rows="4"
                class="code-textarea"
              />
            </el-form-item>

            <el-form-item label="自定义底部代码">
              <el-input
                v-model="config.customFooter"
                type="textarea"
                placeholder="输入自定义底部代码..."
                :rows="4"
                class="code-textarea"
              />
            </el-form-item>

            <el-form-item label="网站维护模式">
              <el-switch
                v-model="config.maintenanceMode"
                active-text="开启"
                inactive-text="关闭"
              />
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>

    <!-- 底部操作按钮 -->
    <div class="action-buttons">
      <el-button class="save-btn" type="primary" @click="saveConfig">
        <el-icon><Check /></el-icon>
        <span>保存配置</span>
      </el-button>
      <el-button class="reset-btn" @click="resetConfig">
        <el-icon><Refresh /></el-icon>
        <span>恢复默认</span>
      </el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import {
  Setting,
  Picture,
  Upload,
  Sunny,
  Moon,
  Close,
  Plus,
  Check,
  Refresh,
  Promotion,
  Platform,
  Camera,
  Connection,
  Position,
  VideoCamera,
  Film,
  ChatLineRound,
  Iphone,
  Link,
} from "@element-plus/icons-vue";

// 配置数据结构
interface SiteConfig {
  siteName: string;
  siteLogo: string;
  siteDescription: string;
  siteKeywords: string;
  primaryColor: string;
  secondaryColor: string;
  themeMode: "light" | "dark";
  seoTitle: string;
  metaDescription: string;
  metaKeywords: string;
  googleVerification: string;
  socialLinks: { platform: string; url: string }[];
  googleAnalytics: string;
  customHead: string;
  customFooter: string;
  maintenanceMode: boolean;
  fontFamily: string;
}

// 默认配置
const defaultConfig: SiteConfig = {
  siteName: "我的网站",
  siteLogo: "",
  siteDescription: "一个简洁现代的网站",
  siteKeywords: "网站,博客,个人",
  primaryColor: "#4a6cf7",
  secondaryColor: "#6c5ce7",
  themeMode: "light",
  seoTitle: "我的网站 - 简洁现代的设计",
  metaDescription: "一个简洁现代的网站，提供优质内容和服务",
  metaKeywords: "网站,博客,个人",
  googleVerification: "",
  socialLinks: [
    { platform: "twitter", url: "" },
    { platform: "facebook", url: "" },
    { platform: "instagram", url: "" },
  ],
  googleAnalytics: "",
  customHead: "",
  customFooter: "",
  maintenanceMode: false,
  fontFamily: "'Segoe UI', sans-serif",
};

// 当前配置
const config = reactive<SiteConfig>({ ...defaultConfig });

// 配置区域
const sections = ref([
  { title: "基本设置", icon: Setting },
  { title: "主题设置", icon: Picture },
  { title: "SEO设置", icon: Promotion },
  { title: "社交链接", icon: Connection },
  { title: "高级设置", icon: Platform },
]);

// 当前激活的区域
const activeSection = ref(0);

// Logo上传相关
const logoInput = ref<HTMLInputElement | null>(null);

// 处理菜单选择
const handleMenuSelect = (index: string) => {
  activeSection.value = parseInt(index);
};

// 获取社交平台对应的图标组件
const getSocialIcon = (platform: string) => {
  const iconMap: Record<string, any> = {
    twitter: Promotion,
    facebook: Platform,
    instagram: Camera,
    github: Connection,
    linkedin: Position,
    youtube: VideoCamera,
    tiktok: Film,
    wechat: ChatLineRound,
    whatsapp: Iphone,
    custom: Link,
  };
  return iconMap[platform] || Link;
};

// 触发Logo上传
function triggerLogoUpload() {
  if (logoInput.value) {
    logoInput.value.click();
  }
}

// 处理Logo上传
function handleLogoUpload(event: Event) {
  const input = event.target as HTMLInputElement;
  if (input.files && input.files[0]) {
    const file = input.files[0];
    const reader = new FileReader();

    reader.onload = (e) => {
      if (e.target?.result) {
        config.siteLogo = e.target.result as string;
      }
    };

    reader.readAsDataURL(file);
  }
}

// 添加社交链接
function addSocialLink() {
  config.socialLinks.push({ platform: "custom", url: "" });
}

// 移除社交链接
function removeSocialLink(index: number) {
  config.socialLinks.splice(index, 1);
}

// 保存配置
function saveConfig() {
  localStorage.setItem("siteConfig", JSON.stringify(config));
  ElMessage.success("配置保存成功！");
}

// 恢复默认配置
function resetConfig() {
  ElMessageBox.confirm("确定要恢复默认配置吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      Object.assign(config, defaultConfig);
      localStorage.removeItem("siteConfig");
      ElMessage.success("已恢复默认配置");
    })
    .catch(() => {
      // 用户取消操作
    });
}

// 初始化加载配置
onMounted(() => {
  const savedConfig = localStorage.getItem("siteConfig");
  if (savedConfig) {
    try {
      const parsedConfig = JSON.parse(savedConfig);
      Object.assign(config, parsedConfig);
    } catch (e) {
      console.error("加载配置失败", e);
    }
  }
});
</script>

<style lang="scss" scoped>
$primary: #4a6cf7;
$secondary: #6c5ce7;
$accent: #ff6b6b;
$light-bg: #f8f9fa;
$text-dark: #333;
$text-light: #666;
$border-color: #e0e0e0;
$card-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
$transition: all 0.3s ease;

.config-container {
  padding: 2rem;
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  background-color: #f5f7ff;
  min-height: 100vh;
}

.config-content {
  display: flex;
  gap: 2rem;
  background: white;
  border-radius: 16px;
  box-shadow: $card-shadow;
  overflow: hidden;
}

.sidebar {
  width: 240px;
  background: white;
  border-right: 1px solid $border-color;

  .config-menu {
    border-right: none;

    .el-menu-item {
      height: 60px;
      display: flex;
      align-items: center;
      font-weight: 500;
      color: $text-light;
      transition: $transition;

      &:hover {
        color: $primary;
        background: rgba($primary, 0.05);
      }

      &.is-active {
        color: $primary;
        background: rgba($primary, 0.1);
        border-left: 3px solid $primary;
      }

      .el-icon {
        margin-right: 10px;
        font-size: 1.2rem;
      }
    }
  }
}

.main-content {
  flex: 1;
  padding: 2rem;
}

.config-section {
  margin-bottom: 2.5rem;
}

.section-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: $text-dark;
  margin-bottom: 1.5rem;
  padding-bottom: 0.8rem;
  border-bottom: 1px solid $border-color;
}

.logo-upload {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.logo-preview {
  width: 120px;
  height: 120px;
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
  border-radius: 8px;
  border: 1px dashed $border-color;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f9f9f9;
}

.logo-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: $text-light;

  .el-icon {
    color: #ccc;
    margin-bottom: 0.5rem;
  }
}

.upload-btn {
  width: fit-content;
}

.file-input {
  display: none;
}

.color-picker {
  display: flex;
  align-items: center;
  gap: 1rem;

  .color-value {
    font-size: 0.9rem;
    color: $text-light;
  }
}

.theme-toggle {
  .el-radio-button {
    display: flex;
    align-items: center;

    .el-icon {
      margin-right: 8px;
    }
  }
}

.social-links {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.social-item {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 10px;
  background: #f9f9f9;
  border-radius: 8px;
}

.social-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #f0f4ff;
  display: flex;
  align-items: center;
  justify-content: center;

  .el-icon {
    color: $primary;
  }
}

.social-input {
  flex: 1;
}

.remove-btn {
  flex-shrink: 0;
}

.add-social-btn {
  margin-top: 0.5rem;
}

.code-textarea {
  font-family: "Courier New", monospace;
  font-size: 0.9rem;
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 1.5rem;
  margin-top: 2rem;
  padding-top: 2rem;
  border-top: 1px solid $border-color;

  .el-button {
    padding: 0.9rem 2rem;
    font-size: 1rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.8rem;

    .el-icon {
      font-size: 1.1rem;
    }
  }

  .save-btn {
    background: $primary;
    color: white;
    border: none;

    &:hover {
      background: darken($primary, 10%);
      transform: translateY(-2px);
      box-shadow: 0 4px 10px rgba($primary, 0.3);
    }
  }

  .reset-btn {
    background: white;
    color: $text-dark;
    border: 1px solid $border-color;

    &:hover {
      background: #f5f5f5;
      border-color: darken($border-color, 10%);
    }
  }
}
</style>